package service

import (
	"gitee.com/linxing_3/sr-core/domain/service/srvctx"
	"gitee.com/mkwchecking/sr-admin/apps/holoHistMsg/acl"
	"gitee.com/mkwchecking/sr-admin/apps/holoHistMsg/entity"
	"gitee.com/mkwchecking/sr-admin/apps/holoHistMsg/repo"
	"github.com/golang-module/carbon"
)

func NewHistoryMsg(msgRepo *repo.HistoryMsg) *HistoryMsg {
	return &HistoryMsg{
		msgRepo: msgRepo,
	}
}

type HistoryMsg struct {
	msgRepo *repo.HistoryMsg
}

func (s *HistoryMsg) GetRoomHistoryMsg(ctx srvctx.IContext, req *acl.HistoryMessageFetchReq) ([]*acl.MessageItem, int64, error) {
	res, total, err := s.msgRepo.GetRoomHistoryMsg("holoim_"+req.Rid,
		req.Keyword,
		carbon.NewCarbon().AddSeconds(req.StartAt).ToStdTime(),
		carbon.NewCarbon().AddSeconds(req.EndAt).ToStdTime(),
		req.PageReq,
	)
	if err != nil {
		return nil, 0, err
	}

	items := make([]*acl.MessageItem, 0)
	for _, msg := range res {
		resMsg := &acl.MessageItem{
			Content: acl.MessageContent{
				Content:     msg.Content,
				ObjectName:  entity.ParseObjectName(msg.Holomongomsg.MessageType),
				Flag:        1,
				OriginalUrl: msg.OriginalUrl,
				ThumbUrl:    msg.ThumbUrl,
				VideoUrl:    msg.VideoUrl,
				UserInfo: acl.MessageUser{
					Id:          msg.UserInfo.Id,
					Username:    msg.UserInfo.Username,
					PortraitUrl: msg.UserInfo.PortraitUrl,
				},
			},
			ConversationType: msg.Holomongomsg.ConversationType,
			Created:          msg.Holomongomsg.SentTime,
			SentDatumTime:    msg.Holomongomsg.SentTime,
			Updated:          msg.Holomongomsg.SentTime,
			Extra:            msg.Holomongomsg.Extra,
			SenderId:         msg.Holomongomsg.SenderId,
			TargetId:         msg.Holomongomsg.TargetId,
			MessageId:        msg.Holomongomsg.MessageId,
			MessageType:      msg.Holomongomsg.MessageType,
		}
		items = append(items, resMsg)
	}

	return items, total, nil
}
